package com.example.eos.mapper;

import com.example.eos.bean.Label;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LabelMapper {

    //查询标签列表
    @Select("select labelid, labelname, usednum, creattime from label where exist = true order by usednum desc, creattime desc")
    List<Label> findAll();

    //查询标签信息（标签id）
    @Select("select labelid, labelname, usednum, creattime from label where labelid = #{labelid,jdbcType=VARCHAR} and exist = true")
    Label findByLabelid(String labelid);

    //查询标签（标签名）
    @Select("select labelid, labelname from label where labelname = #{labelname,jdbcType=VARCHAR} and exist = true")
    Label findByLabelname(String labelname);

    //查询标签列表（标签名）
    @Select("select labelid, labelname from label where labelname like CONCAT(CONCAT('%', #{labelname}), '%') and exist = true  order by usednum desc")
    List<Label> findListByLabelname(String labelname);

    //新建标签
    @Insert("insert into label (labelid, labelname, usednum, creattime, exist) values (#{labelid,jdbcType=VARCHAR}, #{labelname,jdbcType=VARCHAR}, #{usednum,jdbcType=INTEGER}, #{creattime}, #{exist,jdbcType=BOOLEAN})")
    void save(Label label);

    //更新标签使用数
    @Update("update label set usednum = usednum+1 where labelname = #{labelname,jdbcType=VARCHAR} and exist = true")
    void updateLabelUsednum(String labelname);

    //删除标签
    @Update("update label set exist = false where labelid = #{labelid,jdbcType=VARCHAR}")
    void deleteByLabelid(String labelid);

}
